/*
 * ***** BEGIN LICENSE BLOCK *****
 * Version: MPL 1.1
 *
 * The contents of this file are subject to the Mozilla Public License Version
 * 1.1 (the "License"); you may not use this file except in compliance with
 * the License. You may obtain a copy of the License at
 * http://www.mozilla.org/MPL/
 *
 * Software distributed under the License is distributed on an "AS IS" basis,
 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
 * for the specific language governing rights and limitations under the
 * License.
 *
 * The Original Code is Topless JSF.
 *
 * The Initial Developer of the Original Code is
 * Marco Antonio Villalobos, Jr. (mvillalobos@kineteque.com).
 * Portions created by the Initial Developer are Copyright (C) 2010
 * the Initial Developer. All Rights Reserved.
 *
 * Contributor(s):
 *
 * Marco Antonio Villalobos, Jr. (mvillalobos@kineteque.com).
 *
 * ***** END LICENSE BLOCK *****
 */

package kineteque.jsf.topless.components.delegate;

import javax.faces.render.Renderer;
import javax.faces.render.FacesRenderer;
import javax.faces.context.FacesContext;
import javax.faces.component.UIComponent;
import java.io.IOException;

/**
 * An attempt to make a buffer or delegating renderer.
 */
@FacesRenderer(componentFamily = "javax.faces.Panel", rendererType = "topless.jsf.delegate")
@Deprecated
public class DelegateRenderer extends Renderer {

    /**
     * <p>Render the child components of this {@link javax.faces.component.UIComponent}, following
     * the rules described for <code>encodeBegin()</code> to acquire the
     * appropriate value to be rendered.  This method will only be called
     * if the <code>rendersChildren</code> property of this component
     * is <code>true</code>.</p>
     *
     * @param context   {@link javax.faces.context.FacesContext} for the response we are creating
     * @param component {@link javax.faces.component.UIComponent} whose children are to be rendered
     * @throws java.io.IOException  if an input/output error occurs while rendering
     * @throws NullPointerException if <code>context</code>
     *                              or <code>component</code> is <code>null</code>
     */
    @Override
    public void encodeChildren(FacesContext context, UIComponent component) throws IOException {
        UIDelegateContainer delegateContainer = (UIDelegateContainer) component;
        UIComponent delegate = delegateContainer.getDelegate();
        if (delegateContainer.isOnlyRenderDelegateChildren()) {
            for (UIComponent child:delegate.getChildren()) {
                child.encodeAll(context);
            }
        } else {
            delegate.encodeAll(context);
        }
    }

    /**
     * <p>Return a flag indicating whether this {@link javax.faces.render.Renderer} is responsible
     * for rendering the children the component it is asked to render.
     * The default implementation returns <code>false</code>.</p>
     */
    @Override
    public boolean getRendersChildren() {
        return true;
    }
}
